import unittest
import os

def load_all_tests():
    loader = unittest.TestLoader()
    suite = unittest.TestSuite()
    # 搜索 tests 目录下所有 test*.py 文件
    for root, dirs, files in os.walk(os.path.join(os.path.dirname(__file__))):
        for file in files:
            if file.startswith('test') and file.endswith('.py'):
                # 构造模块名（如 tests.test_xxx）
                rel_path = os.path.relpath(os.path.join(root, file), os.path.dirname(__file__))
                module_name = rel_path.replace(os.sep, '.')[:-3]
                try:
                    module = __import__(module_name, fromlist=[''])
                    suite.addTests(loader.loadTestsFromModule(module))
                except Exception as e:
                    print(f'加载 {module_name} 失败: {e}')
    return suite

if __name__ == '__main__':
    runner = unittest.TextTestRunner(verbosity=2)
    all_tests = load_all_tests()
    runner.run(all_tests)
